package com.ziq.zcamp.post

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.zcamp.bean.post.PostMedia
import com.ziq.zcamp.databinding.ItemSelectPictureBinding
import com.ziq.zcamp.util.ImageUtil

class PublishPictureAdapter(list: MutableList<PostMedia>) : RecyclerViewDragAdapter<BindingViewHolder<ItemSelectPictureBinding>, PostMedia, ItemSelectPictureBinding>(list){

    var onItemClick : ((pictureList: ArrayList<PostMedia>, index:Int)->Unit)? = null

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemSelectPictureBinding> {
        return createBindingViewHolder(parent)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemSelectPictureBinding>,
        position: Int
    ) {
        holder.itemView.visibility = View.VISIBLE
        val media = list[position]
        val path= media.getUrl()
        ImageUtil.updateImageFromNetwork(holder.binding.img, path, ImageRequest.CacheChoice.DEFAULT)
        holder.itemView.click {
            val list = ArrayList<PostMedia>()
            for (item in getData()){
                list.add(item)
            }
            onItemClick?.invoke(list, position)
        }

    }

}
